/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package systole.persistence.brokersDB;

import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import systole.domain.clinicalInformation.Family;
import systole.exceptions.ExceptionDAO;
import systole.persistence.FacadeDB;
import systole.persistence.brokersInterface.FamilyBroker;
import systole.view.messages.ErrorMessages;

/**
 *
 * @author jmj
 */
public class FamilyBrokerDB extends BrokerDB implements FamilyBroker {

    /**
     */
    public FamilyBrokerDB() {
        super();
    }

    public void insert(Family family) throws ExceptionDAO {
        try {
            this.logger.logDebug("saving family");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            currentSession.save(family);
            this.logger.logDebug("save family successfully");
        } catch (HibernateException e) {
            this.logger.logError("error on save family, msg: " + e.getMessage());
            throw new ExceptionDAO(ErrorMessages.CHANGES_NOT_SAVE, e.fillInStackTrace());
        }
    }

    public void update(Family family) throws ExceptionDAO {
        try {
            this.logger.logDebug("updating family");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            currentSession.update(family);
            this.logger.logDebug("update family successfully");
        } catch (HibernateException e) {
            this.logger.logError("error on update family, msg: " + e.getMessage());
            throw new ExceptionDAO(ErrorMessages.CHANGES_NOT_SAVE, e.fillInStackTrace());
        }
    }

    public List<Family> getAllFamilies() throws ExceptionDAO {
        try {
            this.logger.logDebug("Getting all family");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            @SuppressWarnings("unchecked")
            List<Family> list = currentSession.createQuery("from Family f order by upper(f.name)").list();
            this.logger.logDebug("get family successfully");
            return list;
        } catch (HibernateException e) {
            this.logger.logError("error on get family, msg: " + e.getMessage());
            throw new ExceptionDAO("No se pudieron obtener los vínculos familiares", e.fillInStackTrace());
        }
    }

    @SuppressWarnings("unchecked")
    public boolean existFamily(String name, Integer id) throws ExceptionDAO {
        try {
            this.logger.logDebug("exist Family by name");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            Criteria criteria = currentSession.createCriteria(Family.class).
                    add((id == null) ? Restrictions.eq("name", name)
                    : Restrictions.and(Restrictions.eq("name", name), Restrictions.ne("id", id)));
            List list = criteria.list();
            this.logger.logDebug("exist Family by name successfully");
            return !list.isEmpty();
        } catch (HibernateException e) {
            this.logger.logError("error on exist Family by name, msg: " + e.getMessage());
            throw new ExceptionDAO("No se pudo verificar si ya existe el vínculo familiar", e.fillInStackTrace());
        }
    }

    public Family getFamilyByName(String name) throws ExceptionDAO {
        try {
            this.logger.logDebug("get Family by name");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            Criteria criteria = currentSession.createCriteria(Family.class).
                    add(Restrictions.eq("name", name)).setMaxResults(1);
            return (Family) criteria.uniqueResult();
        } catch (HibernateException e) {
            this.logger.logError("error on get fmaily by name, msg: " + e.getMessage());
            throw new ExceptionDAO("No se pudo obtener el vínculo familiar", e.fillInStackTrace());
        }
    }
}
